import { IProtocol } from "../../Framework/Protocol/IProtocol";
import { Communicator } from "./Communicator";
//class CommunicatorManager

export class CommunicatorManager  {
  constructor() {
    if (CommunicatorManager.instance != null) {
      throw new Error("singler");
    }
    this._communicators = [];
  }

  public createCommunicator(
    url: string,
    protocol: IProtocol
  ): Communicator {
    var communicator: Communicator;
    var i = 0;
    while (i < this._communicators.length) {
      communicator = this._communicators[i] as Communicator;
      if (communicator.url == url) {
        return communicator;
      }
      ++i;
    }
    communicator = new Communicator(protocol);
    communicator.url = url;
    this._communicators.push(communicator);
    return communicator;
  }

  public removeCommunicator(url: string): Boolean {
    var i = 0;
    while (i < this._communicators.length) {
      if (this._communicators[i].url == url) {
        this._communicators.splice(i, 1);
      }
      ++i;
    }
    return false;
  }

  public static getInstance(): CommunicatorManager {
    if (CommunicatorManager.instance == null) {
      CommunicatorManager.instance = new CommunicatorManager();
    }
    return CommunicatorManager.instance;
  }

  private /* var */ _communicators: Array<Communicator>;

  private static /* var */ instance: CommunicatorManager;
}
